﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using XRpgLibrary;
using XRpgLibrary.GraphicsClasses;
using XRpgLibrary.TileEngine;
using XRpgLibrary.ExtensionMethods;
using XRpgLibrary.Controls;

namespace XRpgLibrary.InterfaceClasses
{
    public class MenuBox : BaseMenuComponent, IGameDrawable, IGameUpdateable
    {
        const int DEFAULT_WIDTH = 960;
        const int DEFAULT_HEIGHT = 320;
        public Vector4 Padding = new Vector4(16, 16, 16, 16);
        protected ControlManager controls;

        public MenuBox(RpgGame game, ControlManager controls, int x, int y, int width, int height, Vector4 padding)
            : base(game, new Rectangle(x, y, width, height), true)
        {
            Padding = padding;
            Construct(controls);
        }

        public MenuBox(RpgGame game, ControlManager controls, int x, int y, int width, int height)
            : base(game, new Rectangle(x, y, width, height), true)
        {
            Construct(controls);
        }


        public MenuBox(RpgGame game, ControlManager controls, MenuPlacementX x, MenuPlacementY y, int width, int height)
            : base(game, x, y, width, height, true)
        {
            Construct(controls);
        }

        public MenuBox(RpgGame game, ControlManager controls, MenuPlacementX x, MenuPlacementY y)
            : base(game, x, y, DEFAULT_WIDTH, DEFAULT_HEIGHT, true)
        {
            Construct(controls);
        }

        public MenuBox(RpgGame game, ControlManager controls)
            : base(game, MenuPlacementX.Center, MenuPlacementY.Center, DEFAULT_WIDTH, DEFAULT_HEIGHT, true)
        {
            Construct(controls);
        }

        public MenuBox(RpgGame game)
            : base(game, MenuPlacementX.Center, MenuPlacementY.Center, DEFAULT_WIDTH, DEFAULT_HEIGHT, true)
        { }

        protected void Construct(ControlManager controls)
        {
            this.controls = controls;
            Initialize();
        }

        public override void Initialize()
        {
            for(int i = 0, cnt = controls.Count; i < cnt; i ++)
            {
                controls[i].ParentPosition = new Vector2(MenuRectangle.X, MenuRectangle.Y);
                controls[i].Position = new Vector2(Padding.X, Padding.Y + i * MenuFont.LineSpacing);
            }

            base.Initialize();
        }

        public override void Draw(GameTime gameTime, Camera camera)
        {
            this.Draw(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime, null);

            if (Visible && Enabled)
                foreach (Control ctrl in controls)
                    ctrl.Draw(spriteBatch);
        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            controls.Update(gameTime, PlayerIndex.One);
        }
    }
}
